﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;

namespace HyperWPF.Windows.UseData
{
    /// <summary>
    /// UseDataWindow.xaml 的交互逻辑
    /// </summary>
    public partial class UseDataWindow : Window
    {

        private int _showrowcount;

        /// <summary>
        /// 可以显示的行数
        /// </summary>
        public int ShowRowCount
        {
            get => _showrowcount;
            set { _showrowcount = value; }
        }


        private int _startrow;
        public int StartRow
        {
            get => _startrow;
            set { _startrow = value; }
        }

        private int _startcolumn;
        public int StartColumn
        {
            get => _startcolumn;
            set { _startcolumn = value; }
        }

        /// <summary>
        /// 显示的数
        /// </summary>
        ObservableCollection<RowItem> items = new ObservableCollection<RowItem>();

        public UseDataWindow()
        {
            InitializeComponent();
            datagrid.ItemsSource = items;
            AllData.MyUseData.DataChanged += ShowChangeData;
        }

        private void ButtonFresh_Click(object sender, RoutedEventArgs e)
        {
            NotifeData();
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            if (AllData.MyUseData.UserSetRows != (int)numRows.Value || AllData.MyUseData.UserSetColumns != (int)numColum.Value)
            { 
             AllData.MyUseData.UserSetRows = (int)numRows.Value;
             AllData.MyUseData.UserSetColumns = (int)numColum.Value;
                AllData.MyUseData.Init();
            }


            ObservableCollection < RowItem > items1 = OverallMiniTool.DeepCopy(items);


            for (int i = StartRow; i < StartRow+ShowRowCount; i++)
            {
                for (int j = StartColumn;j<StartColumn+10;j++)
                {
                    AllData.MyUseData.SetData(i,j , items1[i-StartRow]._cells[j- StartColumn]);
                }
            }
            Fresh();
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // DataGrid实际高度
            double dgHeight = datagrid.ActualHeight;

            // 列头高度
            double headerHeight = datagrid.ColumnHeaderHeight;

            // 行高固定30
            double rowHeight = datagrid.RowHeight;

            // 计算可见行数
            ShowRowCount = (int)((dgHeight - headerHeight) / rowHeight);

            if (items.Count != ShowRowCount)
            {
                while (ShowRowCount > items.Count)
                {
                    RowItem ss = new RowItem("");
                    items.Add(ss);
                }
                while (ShowRowCount < items.Count)
                {
                    items.RemoveAt(items.Count - 1);
                }
                Fresh();
            }
        }


        public void Fresh()
        {
            numRows.Value = AllData.MyUseData.UserSetRows;
            numColum.Value = AllData.MyUseData.UserSetColumns;
            CurrentnumRows.Value = StartRow;
            CurrentnumColumns.Value = StartColumn;
            DataVerticalScollar.Maximum = AllData.MyUseData.UserSetRows - ShowRowCount;
            DataHorizonScollar.Maximum=AllData.MyUseData.UserSetColumns-10;
            NotifeData();
        }


        private void ShowChangeData(object? sender, EventArgs e)
        {
            int[]? ss = sender as int[];
            if (ss[0] >= StartRow  && ss[0] < StartRow + ShowRowCount)
            {
                if (ss[1] >= StartColumn && ss[1] < StartColumn + 10)
                {
                    NotifeData();
                }
            }
        }
        private void NotifeData()
        {
            for (int i = 0; i < ShowRowCount; i++)
            {
                if (i < items.Count)
                {
                    items[i].Cell0 = AllData.MyUseData.GetData(i + StartRow, StartColumn);
                    items[i].Cell1 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 1);
                    items[i].Cell2 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 2);
                    items[i].Cell3 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 3);
                    items[i].Cell4 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 4);
                    items[i].Cell5 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 5);
                    items[i].Cell6 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 6);
                    items[i].Cell7 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 7);
                    items[i].Cell8 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 8);
                    items[i].Cell9 = AllData.MyUseData.GetData(i + StartRow, StartColumn + 9);
                }
            }
        }

        private void DataVerticalScollar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            StartRow = (int)e.NewValue;
            NotifeData();
        }

        private void DataHorizonScollar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            StartColumn = (int)e.NewValue;
            NotifeData();
        }

        private void datagrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells.Count ==1)
            {
                DataGridCellInfo cellInfo = e.AddedCells[0];
                int rowIndex = datagrid.Items.IndexOf(cellInfo.Item) + StartRow; // 行索引
                int columnIndex = cellInfo.Column.DisplayIndex + StartColumn;
                CurrentnumRows.Value = rowIndex;
                CurrentnumColumns.Value = columnIndex;
            }
            else
            {
                CurrentnumRows.Value = -1;
                CurrentnumColumns.Value = -1;
            }
        }
    }

















    public class RowItem : INotifyPropertyChanged
    {
        public string[] _cells = new string[10] { "","","","","","","","","",""};
        public string Cell0
        {
            get => _cells[0] ?? "";
            set
            {
                if (_cells[0] != value)
                {
                    _cells[0] = value;
                    OnPropertyChanged(nameof(Cell0));
                }
            }
        }
        public string Cell1
        {
            get => _cells[1] ?? "";
            set
            {
                if (_cells[1] != value)
                {
                    _cells[1] = value;
                    OnPropertyChanged(nameof(Cell1));
                }
            }
        }
        public string Cell2
        {
            get => _cells[2] ?? "";
            set
            {
                if (_cells[2] != value)
                {
                    _cells[2] = value;
                    OnPropertyChanged(nameof(Cell2));
                }
            }
        }
        public string Cell3
        {
            get => _cells[3] ?? "";
            set
            {
                if (_cells[3] != value)
                {
                    _cells[3] = value;
                    OnPropertyChanged(nameof(Cell3));
                }
            }
        }
        public string Cell4
        {
            get => _cells[4] ?? "";
            set
            {
                if (_cells[4] != value)
                {
                    _cells[4] = value;
                    OnPropertyChanged(nameof(Cell4));
                }
            }
        }
        public string Cell5
        {
            get => _cells[5] ?? "";
            set
            {
                if (_cells[5] != value)
                {
                    _cells[5] = value;
                    OnPropertyChanged(nameof(Cell5));
                }
            }
        }
        public string Cell6
        {
            get => _cells[6] ?? "";
            set
            {
                if (_cells[6] != value)
                {
                    _cells[6] = value;
                    OnPropertyChanged(nameof(Cell6));
                }
            }
        }
        public string Cell7
        {
            get => _cells[7] ?? "";
            set
            {
                if (_cells[7] != value)
                {
                    _cells[7] = value;
                    OnPropertyChanged(nameof(Cell7));
                }
            }
        }
        public string Cell8
        {
            get => _cells[8] ?? "";
            set
            {
                if (_cells[8] != value)
                {
                    _cells[8] = value;
                    OnPropertyChanged(nameof(Cell8));
                }
            }
        }
        public string Cell9
        {
            get => _cells[9] ?? "";
            set
            {
                if (_cells[9] != value)
                {
                    _cells[9] = value;
                    OnPropertyChanged(nameof(Cell9));
                }
            }
        }


        public RowItem(string node)
        {
            Cell0 = node;
            Cell1 = node;
            Cell2 = node;
            Cell3 = node;
            Cell4 = node;
            Cell5 = node;
            Cell6 = node;
            Cell7 = node;
            Cell8 = node;
            Cell9 = node;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
